﻿using Solidinfo.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Solidinfo.Web.Helpers
{
    public static class PagerHelper
    {

        public static MvcHtmlString EditForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression)
            where TFilter : new()
            where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var exprssionStr = string.Format("Filter.{0}", str);
            //var m = ModelMetadata.FromStringExpression(exprssionStr, html.ViewData);
            //FilterTemplate
            return EditorExtensions.Editor(html, exprssionStr);
        }
        public static MvcHtmlString EditForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression,string templateName)
           where TFilter : new()
           where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var exprssionStr = string.Format("Filter.{0}", str);
            return EditorExtensions.Editor(html, exprssionStr,templateName,null);
        }

        public static MvcHtmlString TextBoxForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression)
            where TFilter : new()
            where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var parameter = Expression.Parameter(typeof(IPager<TModel, TFilter>), "e");
            var resultExpression = Expression.Lambda(Expression.Property(Expression.Property(parameter, "Filter"), str), parameter);
            return html.TextBoxFor<IPager<TModel, TFilter>, TValue>((Expression<Func<IPager<TModel, TFilter>, TValue>>)resultExpression);
        }

        public static MvcHtmlString TextBoxForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression, object htmlAttributes)
            where TFilter : new()
            where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var parameter = Expression.Parameter(typeof(IPager<TModel, TFilter>), "e");
            var resultExpression = Expression.Lambda(Expression.Property(Expression.Property(parameter, "Filter"), str), parameter);
            return html.TextBoxFor<IPager<TModel, TFilter>, TValue>((Expression<Func<IPager<TModel, TFilter>, TValue>>)resultExpression, htmlAttributes);
        }

        public static MvcHtmlString HiddenForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression)
            where TFilter : new()
            where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var parameter = Expression.Parameter(typeof(IPager<TModel, TFilter>), "e");
            var resultExpression = Expression.Lambda(Expression.Property(Expression.Property(parameter, "Filter"), str), parameter);
            return html.HiddenFor<IPager<TModel, TFilter>, TValue>((Expression<Func<IPager<TModel, TFilter>, TValue>>)resultExpression);
        }

        public static MvcHtmlString HiddenForFilter<TModel, TFilter, TValue>(this HtmlHelper<IPager<TModel, TFilter>> html, Expression<Func<TFilter, TValue>> expression, object htmlAttributes)
            where TFilter : new()
            where TModel : class
        {
            var te = expression.Body as MemberExpression;
            var str = te.Member.Name;
            var parameter = Expression.Parameter(typeof(IPager<TModel, TFilter>), "e");
            var resultExpression = Expression.Lambda(Expression.Property(Expression.Property(parameter, "Filter"), str), parameter);
            return html.HiddenFor<IPager<TModel, TFilter>, TValue>((Expression<Func<IPager<TModel, TFilter>, TValue>>)resultExpression, htmlAttributes);
        }


        public static MvcHtmlString PagerFor<TModel, TFilter>(this HtmlHelper<IPager<TModel, TFilter>> html, IPager model)
            where TFilter : new()
            where TModel : class
        {
            return html.Partial("_Pager", model);
        }

        public static MvcHtmlString PagerFor<TModel>(this HtmlHelper<IPager<TModel>> html, IPager model)
            where TModel : class
        {
            return html.Partial("_Pager", model);
        }
        public static MvcHtmlString SmallPagerFor<TModel, TFilter>(this HtmlHelper<IPager<TModel, TFilter>> html, IPager model)
            where TFilter : new()
            where TModel : class
        {
            return html.Partial("_SmallPager", model);
        }

        public static MvcHtmlString SmallPagerFor<TModel>(this HtmlHelper<IPager<TModel>> html, IPager model)
            where TModel : class
        {
            return html.Partial("_SmallPager", model);
        }
    }
}